﻿using BlueportCoffee.RobotModel;
using System;
using System.IO.Ports;
using System.Linq;
using System.Threading;

namespace BlueportCoffee.model
{
    public class IOMsg
    {
        private static IOMsg instance;
        private IOMsg() { }

        public static IOMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsg();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool RobotBol = true;
        public bool isRobotBol = false;
        public Thread All_thr;
        /// <summary>
        /// 左臂点位全部关闭
        /// </summary>
        public void QK()
        {
            try
            {
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotKSZZ_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotKFP_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotNCP_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotHHP_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQBJC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotWBTZ_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJBCG_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJB_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQKFJ_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQNCJ_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQKS_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQYM_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQYG_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQTZ_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC1_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC2_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC3_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC4_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotCBWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotCKFWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotCNCWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotKSWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotTZWC_Down();
                Thread.Sleep(100);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void RobotQKAll()
        {
            try
            {
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRTZYXL_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRYXL_Down();
                Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRBJQRL_Down();
                //Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRSDL_Down();
                //Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRXDL_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRSNL_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRXSNL_Down();
                Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRZTL_Down();
                //Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRJXYXL_Down();
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 机器人功能按钮关闭
        /// 状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        public void RobotQK(double type = 12)
        {
            try
            {
                if (type == 12 || type == 11)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRTZYXL_Down();
                }
                if (type == 12 || type == 7)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRYXL_Down();
                }
                if (type == 12 || type == 9)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRBJQRL_Down();
                }
                //if (type == 12 || type == 3)
                //{
                //    Thread.Sleep(100);
                //    RobotModelLeft.Instance.RobotJQRSDL_Down();
                //}
                //if (type == 12 || type == 4)
                //{
                //    Thread.Sleep(100);
                //    RobotModelLeft.Instance.RobotJQRXDL_Down();
                //}
                if (type == 12 || type == 5)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRSNL_Down();
                }
                if (type == 12 || type == 6)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRXSNL_Down();
                }
                //if (type == 12 || type == 10)
                //{
                //    Thread.Sleep(100);
                //    RobotModelLeft.Instance.RobotJQRZTL_Down();
                //}
                //if (type == 12 || type == 8)
                //{
                //    Thread.Sleep(100);
                //    RobotModelLeft.Instance.RobotJQRJXYXL_Down();
                //}
                Thread.Sleep(100);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void IOSerial_Robot_Thr()
        {
            try
            {
                bool isRobot = false;
                RobotSerialPortLeft.Instance.Initialization(IPorPortInfo.Instance.RobotIPLeft, IPorPortInfo.Instance.RobotPortLeft);
                Instance.RobotQK();
                Thread.Sleep(100);
                //RobotModelLeft.Instance.RobotJQRSDL_UP();
                //Thread.Sleep(1000);
                //RobotModelLeft.Instance.RobotJQRSDL_Down();
                //Thread.Sleep(100);
                while (!isRobot)
                {
                    isRobot = RobotModelLeft.Instance.LRobotJQRYSN_Sel();
                    Thread.Sleep(100);
                }
                Instance.QK();
               
                isRobotBol = true;
            }
            catch (Exception)
            {
            }
        }
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortName)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IOSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.PortName, 38400, "", 8, 1);//其他串口通讯
            }
            catch (Exception)
            {
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
     

        #endregion
    }
}
